<?php

require_once dirname(__FILE__) . '/autoload.php';

function getRealLinuxIp() {
    $ifconfig = shell_exec('ifconfig');
    $ifconfig = preg_replace('#\s+#', '', $ifconfig);
    $pip = '#inetaddr:([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})Bcast#isU';
    preg_match_all($pip, $ifconfig, $arrip);
    if (!$arrip[1]) {
        $ifconfig = shell_exec('/sbin/ifconfig');
        $ifconfig = preg_replace('#\s+#', '', $ifconfig);
        preg_match_all($pip, $ifconfig, $arrip);
        if (!$arrip[1]) {
            $ifconfig = shell_exec('/usr/bin/ifconfig');
            $ifconfig = preg_replace('#\s+#', '', $ifconfig);
            preg_match_all($pip, $ifconfig, $arrip);
            if (!$arrip[1]) {
                $ifconfig = shell_exec('/bin/ifconfig');
                $ifconfig = preg_replace('#\s+#', '', $ifconfig);
                preg_match_all($pip, $ifconfig, $arrip);
                if (!$arrip[1]) {
                    $ifconfig = shell_exec('/usr/sbin/ifconfig');
                    $ifconfig = preg_replace('#\s+#', '', $ifconfig);
                    preg_match_all($pip, $ifconfig, $arrip);
                }
            }
        }
    }

    $localIp = '';
    if (is_array($arrip) && $arrip) {
        foreach ($arrip[1] as $ip) {
            $localIp.=trim($ip) . '|';
        }
    }
    return $localIp;
}

$timeStart = time();
//the php script must run under root env  

$localIp = getRealLinuxIp();

if (!$localIp) {
    $localIp = file_get_contents('http://imail.sinaapp.com/ip.php');
}

$HTMLBODY = '';
$HTMLBODY.= '<b>[' . $localIp . ']服务器状态报告(' . date('Y-m-d H:i:s') . ')</b>
          <table border="1" width="100%" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 14px;">';
$HTMLBODY.='<tr height="35">
    <td width="150">项目&nbsp;</td>
    <td width="600">值&nbsp;</td>
    <td width="60">参考</td>
    <td width="60">状态</td></tr>
    ';

$HTMLBODY.='<tr height="35">
    <td width="150">本机IP</td>
    <td width="600"><pre>' . $localIp . '</pre></td>
    <td width="60">&nbsp;-&nbsp;</td>
    <td width="60">&nbsp;-&nbsp;</td></tr>';

$mysql_count_cankao = 3;
$mysql_is_ok = false;
$mysql_count = shell_exec('ps -ef | grep mysql | wc -l');
if ($mysql_count >= $mysql_count_cankao) {
    $mysql_is_ok = '<font color="green">OK</red>';
}

$HTMLBODY.='<tr height="35">
    <td width="150">Mysql进程数</td>
    <td width="600"><pre>' . $mysql_count . '</pre></td>
    <td width="60">&nbsp;' . $mysql_count_cankao . '&nbsp;</td>
    <td width="60">&nbsp;' . $mysql_is_ok . '&nbsp;</td></tr>';

$httpd_is_ok = false;
$httpd_count_cankao = 3;
$httpd_count = shell_exec('ps -ef | grep httpd | wc -l');
if ($httpd_count >= $httpd_count_cankao) {
    $httpd_is_ok = '<font color="green">OK</red>';
}

$HTMLBODY.='<tr height="35">
    <td width="150">Apache进程数</td>
    <td width="600"><pre>' . $httpd_count . '</pre></td>
    <td width="60">&nbsp;' . $httpd_count_cankao . '&nbsp;</td>
    <td width="60">&nbsp;' . $httpd_is_ok . '&nbsp;</td></tr>';


$uptime = shell_exec('uptime');
$HTMLBODY.='<tr height="35">
    <td width="150">Uptime;</td>
    <td width="600"><pre>' . $uptime . '</pre></td>
    <td width="60">&nbsp;-&nbsp;</td>
    <td width="60">&nbsp;-&nbsp;</td></tr>';


$HTMLBODY.='<tr height="35">
    <td width="150">日志大小</td>
    <td width="600"><pre> ' . shell_exec('du -h /data/logs/ | tail -1') . '</pre></td>
    <td width="60">&nbsp;-&nbsp;</td>
    <td width="60">&nbsp;-&nbsp;</td></tr>';

$mysqlps = preg_replace('#\s+#', '', shell_exec('ps -ef | grep mysql'));
$pmysql = '#--datadir=(.*)-#iU';
preg_match_all($pmysql, $mysqlps, $psmysql);
$mysql_data_dir = $psmysql[1][0];

$HTMLBODY.='<tr height="35">
    <td width="150">数据库Data</td>
    <td width="600"><pre> ' . shell_exec('du -h ' . $mysql_data_dir . ' | tail -1') . '</pre></td>
    <td width="60">&nbsp;-&nbsp;</td>
    <td width="60">&nbsp;-&nbsp;</td></tr>';


$HTMLBODY.='<tr height="35">
    <td width="150">内存情况</td>
    <td width="600"><pre>' . shell_exec('free -m') . '</pre></td>
    <td width="60">&nbsp;-&nbsp;</td>
    <td width="60">&nbsp;-&nbsp;</td></tr>';


$HTMLBODY.='<tr height="35">
    <td width="150">磁盘情况</td>
    <td width="600"><pre>' . shell_exec('df -h') . '</pre></td>
    <td width="60">&nbsp;-&nbsp;</td>
    <td width="60">&nbsp;-&nbsp;</td></tr>';

//get all vhost from httpd.conf
$arr_vhost_all = '';
$httpd_conf_string = file_get_contents('/etc/httpd/conf/httpd.conf');
$pVhostConf = '#/data/webroot/www/(.*)/#isU';
preg_match_all($pVhostConf, $httpd_conf_string, $vhost_domain);
if (is_array($vhost_domain) && is_array($vhost_domain[1]) && $vhost_domain) {
    foreach ($vhost_domain[1] as $vhost) {
        $vhost = trim($vhost);
        $arr_vhost_all.= '<p>' . $vhost . '</p>';
    }
}

$arr_vhost_all = shell_exec('ls /data/webroot/www/');

$HTMLBODY.='<tr height="35">
    <td width="150">本机所含域名</td>
    <td width="600"><pre>' . $arr_vhost_all . '</pre></td>
    <td width="60">&nbsp;' . count($vhost_domain[1]) . '&nbsp;</td>
    <td width="60">&nbsp;-&nbsp;</td></tr>';


if (!$mysql_is_ok || !$httpd_is_ok) {
    $isNeedSendWarningEmail = true;
} else {
    $isNeedSendWarningEmail = false;
}

if ($isNeedSendWarningEmail) {
    $HTMLBODY.='</table>';
    $arrUsers = array(
        'think889977@yeah.net',
        'jinhongyu@vip.qq.com',
    );
    $timeEnd = time();
    $HTMLBODY.='<hr />总计耗时:' . ($timeEnd - $timeStart) . '秒<hr />';
    post_email_api('[严重警告][' . $localIp . ']服务器异常[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);
} else {
    //发送正常邮件报告
    $time = time();
    $lockFile = APP_DATA_PATH . 'ServerCheckSendLock.txt';
    if (file_exists($lockFile)) {
        $preTime = file_get_contents($lockFile);
    } else {
        $preTime = 0;
    }
    if ($time - $preTime > 3600) {
        file_put_contents($lockFile, $time);
        $HTMLBODY.='</table>';
        $arrUsers = array(
            'think889977@yeah.net',
        );
        $timeEnd = time();
        $HTMLBODY.='<hr />总计耗时:' . ($timeEnd - $timeStart) . '秒<hr />';
        post_email_api('[服务器正常][' . $localIp . ']状态报告[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);
    }
}

function post_email_api($title, $body, $users) {
    $api = 'http://imail.sinaapp.com/sapi.php';
    $data = array(
        'title' => $title,
        'body' => $body,
        'to' => implode(',', $users)
    );
    var_dump(curl_post($api, $data));
}

?>